﻿using TDMS.Net.Internals;

namespace TDMS.Net;

public class TdmsChannelGroup : TdmsContainerObject<TdmsChannel>
{
    public TdmsChannelGroup(IntPtr handle, TdmsFile parent, string name, string description)
    {
        Parent = parent;
        Handle = handle;
        Name = name;
        Description = description;
    }

    public TdmsChannel CreateChannel(string name, string description, string unit, ETdmsDataType dataType)
    {
        var handle = new IntPtr();
        ExceptionBuilder.CheckApiStatus(NiLibDDC.DDC_AddChannel(Handle, dataType, name, description, unit, ref handle));
        var channel = new TdmsChannel(handle, this, name, description, dataType);
        Children.Add(channel);
        return channel;
    }

    public void Remove()
    {
        ExceptionBuilder.CheckApiStatus(NiLibDDC.DDC_RemoveChannelGroup(Handle));
    }
}

public static class TdmsChannelGroupExtensions
{
    public static TdmsChannel CreateChannelInt32(this TdmsChannelGroup group, string name, string description, string unit)
        => group.CreateChannel(name, description, unit, ETdmsDataType.Int32);

    public static TdmsChannel CreateChannelFloat(this TdmsChannelGroup group, string name, string description, string unit)
        => group.CreateChannel(name, description, unit, ETdmsDataType.Float);

    public static TdmsChannel CreateChannelDouble(this TdmsChannelGroup group, string name, string description, string unit)
        => group.CreateChannel(name, description, unit, ETdmsDataType.Double);

    public static TdmsChannel CreateChannelString(this TdmsChannelGroup group, string name, string description, string unit)
        => group.CreateChannel(name, description, unit, (ETdmsDataType)23);
}